/*
 * @FileName: 
 * @Version: 
 * @Author: 王杨洋
 * @Date: 2021-06-13 16:29:32
 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include "../include/message.h"
#include "../include/device_manage.h"
#include "../include/media.h"
#define NAME_PASSWORD_LEN 128

// #define DEBUG
static int GetCurSeq(void)
{
	static int cur_seq = 0;
	cur_seq++;
	if(cur_seq >= 0xFF)
	{
		cur_seq = 0;
	}
	return cur_seq;
}

static int GetChkSum(char* data, int dataLen)
{
	int sum = 0;
	for(int i = 0; i < dataLen; i++)
	{
		sum += data[i];
	}
	return sum;
}

int DoDeviceNotice(int sockfd, MsgStruct *msg, const char *name, char *mac)
{
	int preSeq = GetCurSeq();
    msg->version = MSG_VERSION;
	msg->header_len = MSG_HEADER_STABLE_LEN;
	msg->encrypt_type = MSG_ENCRYPT_NONE;
	msg->protocol_type = MSG_PROTOCOL_C2S;
	msg->data_type = MSG_DATA_GET_ALARM_NOTICE;
	msg->seq_num = preSeq;
	msg->frag_flag = MSG_FLAG_FRAG_NO;
	msg->frag_offset = 0;
    msg->custom1 = strlen(name);
	msg->custom2 = 0xFF;
	msg->total_len = MSG_HEADER_STABLE_LEN+msg->custom1+strlen(mac);
	msg->source_addr = 0x7F000001;
	msg->target_addr = 0x7F000001;
	msg->header_chk = GetChkSum((char*)(msg), MSG_HEADER_STABLE_LEN);
    strcpy(msg->data, name);
	strcpy(msg->data + msg->custom1, mac);

    if(send(sockfd, msg, sizeof(MsgStruct), 0) < 0)
	{
		perror("fail to send.\n");
		return -1;
	}
	printf("send ok!\n");

    memset(msg, 0x0, sizeof(MsgStruct));
	if(recv(sockfd, msg, sizeof(MsgStruct), 0) < 0)
	{
		perror("fail to recv.\n");
		return -1;		
	}

	//Check Some Param
	if (msg->version != MSG_VERSION) 
	{
		printf("version error:0x%x.\n", msg->version);
		return -1;		
	}
	if (msg->encrypt_type != MSG_ENCRYPT_NONE) 
	{
		printf("encrypt_type error:0x%x.\n", msg->encrypt_type);
		return -1;		
	}
	if (msg->protocol_type != MSG_PROTOCOL_S2C) 
	{
		printf("protocol_type error:0x%x.\n", msg->protocol_type);
		return -1;	
	}
	if (msg->frag_flag != MSG_FLAG_FRAG_NO) 
	{
		printf("frag_flag error:0x%x.\n", msg->frag_flag);
		return -1;
	}
	if (msg->header_chk != GetChkSum((char*)(msg), msg->header_len)) 
	{
		printf("header_chk error:0x%x.\n", msg->header_chk);
		return -1;
	}
	//请求失败，打印错误信息
	if (msg->custom1 == MSG_CUSTOM1_GET_ALARM_NOTICE_FAILURE) 
	{
		switch (msg->custom2) 
		{
			case MSG_CUSTOM2_GET_ALARM_NOTICE_ACCOUNT: 
			{
				printf("account error.\n");
				return -1;
			}
			case MSG_CUSTOM2_GET_ALARM_NOTICE_CONNECT: 
			{
				printf("connect error.\n");
				return -1;
			}
			case MSG_CUSTOM2_GET_ALARM_NOTICE_DATABASE: 
			{
				printf("database error.\n");
				return -1;
			}
			default: break;
		}
	}
	if (msg->custom1 == MSG_CUSTOM1_GET_ALARM_NOTICE_SUCCESS) 
	{
		printf("Get device notice success\n");
		int noticeNum = msg->custom2;
		printf("NoticeNum=%d\n",noticeNum);
		for(int i=0; i<noticeNum; i++)
		{
			char buffer[50] = {0};
			int noticeLen = 43;
			memcpy(buffer, msg->data+noticeLen*i, noticeLen);
			printf("NoticeData[%d]=%s\n", i, buffer);
		}
		return 0;
	}
    return 0;
}

int main(int argc, const char *argv[])
{
	int alarmSockFd = -1;

	struct sockaddr_in serveraddr;
	int cmd;
	MsgStruct msg;
	memset(&msg, 0x0, sizeof(MsgStruct));

	char name[NAME_PASSWORD_LEN] = "yy";
	char focusMac[32] = "01:11:22:33:44:55:66:77";;
	const char sourceIp[32] = "127.0.0.1";

	//3.Connect Alarm Service
	if((alarmSockFd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		perror("fail to socket.\n");
		return -1;
	}

	bzero(&serveraddr, sizeof(serveraddr));
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_addr.s_addr = inet_addr(sourceIp);
	serveraddr.sin_port = htons(SERVER_ALARM_PORT);

	if(connect(alarmSockFd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
	{
		perror("fail to connect");
		return -1;
	}

    printf("*****************************************************\n");
	printf("*                      6.Notice                     *\n");
	printf("*****************************************************\n");
	printf("Please choose:");
	scanf("%d", &cmd);
	getchar();
	if (cmd == 6) {
		memset(&msg, 0x0, sizeof(MsgStruct));
		DoDeviceNotice(alarmSockFd, &msg, name, focusMac);
	}
	return 0;
}